var uploadVideo = {
    /*单视频上传*/
    singleTpl: helper.heredoc(function () {/*
<div class="upload-video-container item-row">
    <div class="image" style="background-image:url(${info.cover_url})">
        <div class="icon-bar text-center">
            <i class="mdi mdi-delete text-white" onclick="uploadVideo.delVod(this)"></i>
            <i class="mdi mdi-play text-white" onclick="helper.doDialog(this);"
               data-url="/system/vod.${vodType}.video/player.html?video_id=${info.vid}"
               data-title="视频播放"
               data-width="620px"
               data-height="440px"></i>
        </div>
    </div>
</div>
    */}),
    setVod: function (field, info, vodType) {
        var html = juicer(uploadVideo.singleTpl, {info: info, vodType: vodType});
        var textareaObj = $('textarea[name="' + field + '"]');
        var videoBoxObj = textareaObj.closest('.upload-video-box');
        videoBoxObj.find('.upload-list').html(html).show();
        videoBoxObj.find('.upload-video-btn').hide();
        textareaObj.val(JSON.stringify(info));
    },
    delVod: function (obj) {
        var videoBoxObj = $(obj).closest('.upload-video-box');
        videoBoxObj.find('.upload-list').hide();
        videoBoxObj.find('.upload-video-btn').show();
        videoBoxObj.find('textarea').val('');
    },
    /*多视频上传*/
    multiTpl: helper.heredoc(function () {/*
{@each list as value}
    <div class="upload-video-container item-row" data-val='$${value|json2str}'>
        <div class="image" style="background-image: url(${value.cover_url})">
            <div class="icon-bar">
                <i class="mdi mdi-arrow-left text-white" onclick="uploadVideo.upMove(this)"></i>
                <i class="mdi mdi-arrow-right text-white" onclick="uploadVideo.downMove(this)"></i>
                <i class="mdi mdi-delete text-white" onclick="uploadVideo.delRow(this)"></i>
                <i class="mdi mdi-play text-white" onclick="helper.doDialog(this);"
                   data-url="/system/vod.${vodType}.video/player.html?video_id=${value.vid}"
                   data-title="视频播放"
                   data-width="620px"
                   data-height="440px"></i>
            </div>
        </div>
    </div>
{@/each}
    */}),
    setVods: function (field, list, vodType) {
        var html = juicer(uploadVideo.multiTpl, {list: list, vodType: vodType})
        var textareaObj = $('textarea[name="' + field + '"]');
        var videoBoxObj = textareaObj.closest('.upload-video-box');
        videoBoxObj.find('.upload-list').append(html);
        uploadVideo.setVal(videoBoxObj);
    },
    upMove: function (obj) {
        var videoBoxObj = $(obj).closest('.upload-video-box');
        var leftMove = $(obj).closest('.item-row');   //当前行
        var prevMove = leftMove.prev('.item-row');               //上一个同级元素
        if (prevMove.length > 0) {                        //存在上一个同级元素
            prevMove.insertAfter(leftMove);           //就把它移动到前面
        }
        uploadVideo.setVal(videoBoxObj);
    },
    downMove: function (obj) {
        var videoBoxObj = $(obj).closest('.upload-video-box');
        var rightMove = $(obj).closest('.item-row');
        var nextMove = rightMove.next('.item-row');
        if (nextMove.length > 0) {
            nextMove.insertBefore(rightMove);
        }
        uploadVideo.setVal(videoBoxObj);
    },
    delRow: function(obj){
        var videoBoxObj = $(obj).closest('.upload-video-box');
        $(obj).closest('.item-row').remove();
        uploadVideo.setVal(videoBoxObj);
    },
    setVal: function (videoBoxObj) {
        var arr = [];
        videoBoxObj.find('.upload-list>.item-row').each(function () {
            arr.push($(this).data('val'))
        })
        var val = arr.length>0 ? JSON.stringify(arr) : '';
        videoBoxObj.find('textarea').val(val);
    }
};